//牛顿拉夫逊法计算平方根
#include "stdio.h"

/**获取一个数字的绝对值*/
float absoluteValue(float);
/**牛顿拉夫逊法计算平方根*/
float squareRoot(float);

int main(int argc, char const *argv[]) {
  printf("squareRoot(4) = %f\n",squareRoot(4));
  printf("squareRoot(17) = %f\n",squareRoot(17));
  printf("squareRoot(200) = %f\n",squareRoot(200));
  return 0;
}

float absoluteValue(float x){
  if (x<0) {
    return -x;
  }
  return x;
}

float squareRoot(float x){
  const float epsilon = 0.00000001;
  float guess = 1.0;
  while (absoluteValue(guess*guess - x) >= epsilon) {
    guess = (x/guess+guess)/2.0;
  }
  return guess;
}
